package com.zengqingfa.jvm.test.n1;

import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Opcodes;

/**
 * @fileName: MethodAreaTest1
 * @author: zengqf3
 * @date: 2021-6-15 10:26
 * @description: *
 *  演示元空间溢出 -XX:MetaspaceSize=10M -XX:MaxMetaspaceSize=30M
 * 演示永久代内存溢出  -XX:MaxPermSize=8m
 */
public class MethodAreaTest1 extends ClassLoader {

    public static void main(String[] args) {
        int count = 0;
        try {
            MethodAreaTest1 test = new MethodAreaTest1();
            while (true) {
                //生成类的二进制字节码工具类
                ClassWriter cw = new ClassWriter(0);
                //版本号  权限修饰符  类名   包名
                cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, "Class" + count, null, "java/lang/Object", null);
                //返回字节码的byte数组
                byte[] code = cw.toByteArray();
                //触发类的加载
                test.defineClass("Class" + count++, code, 0, code.length);
            }
        } finally {
            System.out.println(count);
        }

    }

}
